package com.findyou.controller;

import com.findyou.pojo.Result;
import com.findyou.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Title: FileUploadController
 * @Author 贾金博
 * @Package com.findyou.controller
 * @Date 2024/3/10 9:01
 * @description:
 */
@RestController
@Slf4j
@CrossOrigin // 跨域
public class FileUploadController {

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //把文件存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的 防止覆盖
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("name = {}", originalFilename);
       // file.transferTo(new File("C:\\Users\\Lenovo\\Desktop\\files\\" + filename));
        //调用ali
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
